package insight.Builder;

/**
 * @author huu
 * @create 2025-08-26-18:34
 */
public class SelectBuilder implements SQL.tableStage, SQL.whereStage {
    private String table;
    private String where;
    private String[] columns;
    SelectBuilder(String[] columns){
        this.columns = columns;
    }
    @Override
    public SQL.whereStage table(String table){
        this.table = table;
        return this;
    }
    @Override
    public SQL.whereStage where(String where){
        this.where = where;
        return this;
    }
    @Override
    public String build(){
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT ");
        // 修复：检查columns是否为null
        if (columns != null && columns.length > 0) {
            sql.append(String.join(", ", columns));
        } else {
            sql.append("*");
        }
        sql.append(" FROM ");
        sql.append(table);
        // 修复：检查where条件是否存在
        if (where != null && !where.isEmpty()) {
            sql.append(" WHERE ");
            sql.append(where);
        }
        return sql.toString();
    }

}
